package com.happyass.controller;


import com.happyass.pojo.Booking;
import com.happyass.pojo.Ticket;
import com.happyass.service.BookinfoService;
import com.happyass.service.TicketService;
import com.happyass.utils.SmsSend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/booking")
public class BookingController {

    @Autowired
    TicketService ticketService;

    @Autowired
    BookinfoService bookinfoService;

    @Autowired
    SmsSend smsSend;

    //访问购票主页面
    @RequestMapping("/main")
    public String main(Model map){
        //查询票种
        List<Ticket> ticketsList = ticketService.findAll();
        map.addAttribute("ticketsList",ticketsList);
        return "/booking2";
    }

    //查询判断票数/重复/日期
    @RequestMapping("/queryBooking")
    @ResponseBody
    public HashMap<String,Object> upDateGoods(Booking booking, String time, HttpServletRequest request) throws ParseException {
        HashMap<String,Object> map = new HashMap<String,Object>();
        Date bookingTime = new SimpleDateFormat("yyyy-MM-dd").parse(time);
        //判断日期是否合格
        Date nowTime = new Date();
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(nowTime);
        rightNow.add(Calendar.MONTH,1);//日期加1个月
        Date time2 =  rightNow.getTime();
        if(bookingTime.compareTo(nowTime)==-1 || bookingTime.compareTo(time2)==1){
            map.put("info","只能预订从明天起一个月以内的票！");
            return map;
        }
        //查找重复日期的
        List<Booking> bookings = bookinfoService.findAllByDate(bookingTime);
        System.err.println(bookings);
        boolean isRepeat = false;
        for (Booking b:bookings) {
            if(b.getBookingIdenty().equals(booking.getBookingIdenty()) || b.getBookingTel().equals(booking.getBookingTel()))
                isRepeat = true;
        }
        if(isRepeat){
            map.put("info","同一天一个身份证或手机号只能预订一张！");
            return map;
        }
        if(bookings.size()>200){
            map.put("info","该日期的订票已满！请换日期订票！");
            return map;
        }
        booking.setBookingTime(bookingTime);
        booking.setBookdingStatus(0);
        booking.setBookingRefundStatus(0);
        booking.setBookingChangeStatus(0);
        bookinfoService.addDateBooking(booking);
        map.put("info","预订成功！");
        return map;
    }

    //成功预订
    @RequestMapping("/success")
    public String success(Model map){
        return "booking3";
    }

    @RequestMapping("/main2")
    public String main2(){
        return "booking2_1";
    }


    @RequestMapping("/sendSms")
    @ResponseBody
    public HashMap<String,Object> sendSms(String phone, HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<String,Object>();
        Random rd = new Random();
        int code = rd.nextInt(10000);
        //存入session中
        String info = smsSend.send(phone,code+"");
        if (info.equals("OK")) {
            request.getSession().setAttribute("smsCode", code);
            map.put("info", "短信发送成功");
        } else {
            map.put("info", "短信发送失败");
        }
        return map;
    }
    //手机短信登录
    @RequestMapping("/smsLogin")
    @ResponseBody
    public HashMap<String,Object> smsLogin(String tel,String code, HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<String,Object>();
        //String sessionCode = request.getSession().getAttribute("smsCode")+"";
        String sessionCode = "1234";
        if(code.equals(sessionCode)){
            //根据电话去查询用户的名，并存入到session中
            request.getSession().setAttribute("user",tel);
            //查询是否有相关订票
            List<Booking> bookings = bookinfoService.findAllByTel(tel);
            if(bookings.size()<=0){
                map.put("info","未查询到该手机号所购票信息！");
            }else{
                map.put("info","查询成功！");
            }
        }else{
            map.put("info","验证码错误");
        }
        map.put("tel",tel);
        return map;
    }

    //查询页面
    @RequestMapping("queryBookingByTel")
    public String queryBookingByTel(Model map,String tel){
        bookinfoService.upDate();
        List<Booking> bookings = bookinfoService.findAllByTel(tel);
        map.addAttribute("bookings",bookings);
        return "booking3_1";
    }

    //退票
    @RequestMapping("/reBooking")
    @ResponseBody
    public HashMap<String,Object> reBooking(int bookingId, HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<String,Object>();
        Booking booking = bookinfoService.findById(bookingId);
        booking.setBookingRefundStatus(1);
        bookinfoService.addDateBooking(booking);
        map.put("info","退票成功！");
        return map;
    }

    //改签页面
    @RequestMapping("changesBooking")
    public String changesBooking(Model map,int bookingId){
        //获得信息
        Booking booking = bookinfoService.findById(bookingId);
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        String time=formatter.format(booking.getBookingTime());
        map.addAttribute("time",time);
        map.addAttribute("bookingId",bookingId);
        return "booking3_2";
    }

    //改签
    @RequestMapping("/changes")
    @ResponseBody
    public HashMap<String,Object> changes(String time,int bookingId) throws ParseException {
        HashMap<String,Object> map = new HashMap<String,Object>();
        Date bookingTime = new SimpleDateFormat("yyyy-MM-dd").parse(time);
        //判断日期是否合格
        Date nowTime = new Date();
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(nowTime);
        rightNow.add(Calendar.MONTH,1);//日期加1个月
        Date time2 =  rightNow.getTime();
        if(bookingTime.compareTo(nowTime)==-1 || bookingTime.compareTo(time2)==1){
            map.put("info","改签范围为从明天起一个月以内！");
            return map;
        }
        Booking booking = bookinfoService.findById(bookingId);
        if(booking.getBookingChangeStatus()==1){
            map.put("info","已改签！无法再次改签！");
            return map;
        }
        booking.setBookingTime(bookingTime);
        booking.setBookingChangeStatus(1);
        bookinfoService.addDateBooking(booking);
        map.put("info","改签成功！");
        return map;
    }

    @RequestMapping("/bookIndex")
    public String index(){
        return "/booking";
    }

}
